package com.ruoyi.controller.traceability;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.traceability.MesTraceabilityDetail;
import com.ruoyi.service.traceability.IMesTraceabilityDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 追溯详情Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/mes/traceability/detail")
public class MesTraceabilityDetailController extends BaseController {
    @Autowired
    private IMesTraceabilityDetailService mesTraceabilityDetailService;

    /**
     * 查询追溯详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesTraceabilityDetail mesTraceabilityDetail) {
        startPage();
        List<MesTraceabilityDetail> list = mesTraceabilityDetailService.selectMesTraceabilityDetailList(mesTraceabilityDetail);
        return getDataTable(list);
    }

    /**
     * 导出追溯详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:export')")
    @Log(title = "追溯详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesTraceabilityDetail mesTraceabilityDetail) {
        List<MesTraceabilityDetail> list = mesTraceabilityDetailService.selectMesTraceabilityDetailList(mesTraceabilityDetail);
        ExcelUtil<MesTraceabilityDetail> util = new ExcelUtil<MesTraceabilityDetail>(MesTraceabilityDetail.class);
        util.exportExcel(response, list, "追溯详情数据");
    }

    /**
     * 获取追溯详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId) {
        return success(mesTraceabilityDetailService.selectMesTraceabilityDetailByDetailId(detailId));
    }

    /**
     * 新增追溯详情
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:add')")
    @Log(title = "追溯详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesTraceabilityDetail mesTraceabilityDetail) {
        return toAjax(mesTraceabilityDetailService.insertMesTraceabilityDetail(mesTraceabilityDetail));
    }

    /**
     * 修改追溯详情
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:edit')")
    @Log(title = "追溯详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesTraceabilityDetail mesTraceabilityDetail) {
        return toAjax(mesTraceabilityDetailService.updateMesTraceabilityDetail(mesTraceabilityDetail));
    }

    /**
     * 删除追溯详情
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:remove')")
    @Log(title = "追溯详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds) {
        return toAjax(mesTraceabilityDetailService.deleteMesTraceabilityDetailByDetailIds(detailIds));
    }

    /**
     * 根据追溯记录ID查询详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/trace/{traceId}")
    public AjaxResult getByTraceId(@PathVariable Long traceId) {
        List<MesTraceabilityDetail> list = mesTraceabilityDetailService.selectDetailsByTraceId(traceId);
        return success(list);
    }

    /**
     * 根据父节点ID查询子节点列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/parent/{parentNodeId}")
    public AjaxResult getByParentNodeId(@PathVariable Long parentNodeId) {
        List<MesTraceabilityDetail> list = mesTraceabilityDetailService.selectDetailsByParentNodeId(parentNodeId);
        return success(list);
    }

    /**
     * 根据节点类型查询详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:query')")
    @GetMapping("/nodeType/{nodeType}")
    public AjaxResult getByNodeType(@PathVariable String nodeType) {
        List<MesTraceabilityDetail> list = mesTraceabilityDetailService.selectDetailsByNodeType(nodeType);
        return success(list);
    }

    /**
     * 批量插入追溯详情
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:add')")
    @Log(title = "追溯详情", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchInsert(@RequestBody List<MesTraceabilityDetail> traceabilityDetails) {
        return toAjax(mesTraceabilityDetailService.batchInsertTraceabilityDetail(traceabilityDetails));
    }

    /**
     * 根据追溯记录ID删除所有详情
     */
    @PreAuthorize("@ss.hasPermi('mes:traceability:remove')")
    @Log(title = "追溯详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/trace/{traceId}")
    public AjaxResult removeByTraceId(@PathVariable Long traceId) {
        return toAjax(mesTraceabilityDetailService.deleteDetailsByTraceId(traceId));
    }
}